/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_overlap_light.h
 * Desc:    Mesh overlap light shader
 * Version: 1.01
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL
*
\******************************************************************************/

#ifdef OPENGL
	
	#ifndef FRAGMENT_OVERLAP_LIGHT
		#define FRAGMENT_OVERLAP_LIGHT
		
		uniform sampler2D s_texture_0;
		uniform sampler2D s_texture_1;
		uniform sampler2D s_texture_2;
		uniform sampler2D s_texture_3;
		uniform sampler2D s_texture_4;
		uniform sampler2D s_texture_5;
		uniform sampler2D s_texture_6;
		
		uniform half4 mask_transform;
		uniform half4 overlap_transform;
		uniform half mask_value;
		uniform half mask_threshold;
		uniform half angle_value;
		uniform half angle_threshold;
		uniform half4 base_diffuse_color;
		uniform half4 overlap_diffuse_color;
		uniform half base_diffuse_scale;
		uniform half overlap_diffuse_scale;
		uniform half4 base_specular_color;
		uniform half4 overlap_specular_color;
		uniform half base_specular_scale;
		uniform half overlap_specular_scale;
		uniform half base_specular_power;
		uniform half overlap_specular_power;
		uniform half base_phong_rim_width;
		uniform half overlap_phong_rim_width;
		uniform half base_phong_rim_scale;
		uniform half overlap_phong_rim_scale;
		uniform half base_fresnel_bias;
		uniform half overlap_fresnel_bias;
		uniform half base_fresnel_power;
		uniform half overlap_fresnel_power;
		
	#else
		
		#include <core/shaders/default/mesh/fragment_overlap_sample.h>
		
		half3 camera_direction = s_texcoord_2.xyz;
		
		half3 base_shading = s_material_shading * half3(base_diffuse_scale,base_specular_scale,base_specular_power);
		half3 overlap_shading = s_material_shading * half3(overlap_diffuse_scale,overlap_specular_scale,overlap_specular_power);
		
		half3 base_fresnel = half3(base_fresnel_bias,1.0f - base_fresnel_bias,base_fresnel_power);
		half3 overlap_fresnel = half3(overlap_fresnel_bias,1.0f - overlap_fresnel_bias,overlap_fresnel_power);
		
		#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
			half2 base_phong_rim = half2(base_phong_rim_width,base_phong_rim_scale);
			half2 overlap_phong_rim = half2(overlap_phong_rim_width,overlap_phong_rim_scale);
		#endif
		
		#ifdef WORLD
			half3 light_direction = s_texcoord_1.xyz;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 base_color = getDiffuseRim(base_shading,base_phong_rim,base_diffuse.xyz,base_normal,light_direction);
				half3 overlap_color = getDiffuseRim(overlap_shading,overlap_phong_rim,overlap_diffuse.xyz,overlap_normal,light_direction);
			#else
				half3 base_color = getDiffuse(base_shading,base_diffuse.xyz,base_normal,light_direction);
				half3 overlap_color = getDiffuse(overlap_shading,overlap_diffuse.xyz,overlap_normal,light_direction);
			#endif
			base_color += getSpecularFresnelNormalize(base_shading,base_fresnel,base_specular,base_normal,light_direction,camera_direction);
			overlap_color += getSpecularFresnelNormalize(overlap_shading,overlap_fresnel,overlap_specular,overlap_normal,light_direction,camera_direction);
		#else
			half4 light_direction = s_texcoord_1;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 base_color = getDiffuseRimSpecularFresnelNormalizeAttenuate(base_shading,base_phong_rim,base_fresnel,base_diffuse.xyz,base_specular,base_normal,light_direction,camera_direction);
				half3 overlap_color = getDiffuseRimSpecularFresnelNormalizeAttenuate(overlap_shading,overlap_phong_rim,overlap_fresnel,overlap_diffuse.xyz,overlap_specular,overlap_normal,light_direction,camera_direction);
			#else
				half3 base_color = getDiffuseSpecularFresnelNormalizeAttenuate(base_shading,base_fresnel,base_diffuse.xyz,base_specular,base_normal,light_direction,camera_direction);
				half3 overlap_color = getDiffuseSpecularFresnelNormalizeAttenuate(overlap_shading,overlap_fresnel,overlap_diffuse.xyz,overlap_specular,overlap_normal,light_direction,camera_direction);
			#endif
		#endif
		
		half3 color = lerp(base_color,overlap_color,overlap);
		
	#endif
	
/******************************************************************************\
*
* Direct3D11
*
\******************************************************************************/

#elif DIRECT3D11
	
	#ifndef FRAGMENT_OVERLAP_LIGHT
		#define FRAGMENT_OVERLAP_LIGHT
		
		Texture2D s_texture_0 : register(t0);
		Texture2D s_texture_1 : register(t1);
		Texture2D s_texture_2 : register(t2);
		Texture2D s_texture_3 : register(t3);
		Texture2D s_texture_4 : register(t4);
		Texture2D s_texture_5 : register(t5);
		Texture2D s_texture_6 : register(t6);
		
		cbuffer shader_parameters {
			float4 mask_transform;
			float4 overlap_transform;
			float mask_value;
			float mask_threshold;
			float angle_value;
			float angle_threshold;
			float4 base_diffuse_color;
			float4 overlap_diffuse_color;
			float base_diffuse_scale;
			float overlap_diffuse_scale;
			float4 base_specular_color;
			float4 overlap_specular_color;
			float base_specular_scale;
			float overlap_specular_scale;
			float base_specular_power;
			float overlap_specular_power;
			float base_phong_rim_width;
			float overlap_phong_rim_width;
			float base_phong_rim_scale;
			float overlap_phong_rim_scale;
			float base_fresnel_bias;
			float overlap_fresnel_bias;
			float base_fresnel_power;
			float overlap_fresnel_power;
		};
		
	#else
		
		#include <core/shaders/default/mesh/fragment_overlap_sample.h>
		
		half3 camera_direction = IN.texcoord_2.xyz;
		
		half3 base_shading = s_material_shading * half3(base_diffuse_scale,base_specular_scale,base_specular_power);
		half3 overlap_shading = s_material_shading * half3(overlap_diffuse_scale,overlap_specular_scale,overlap_specular_power);
		
		half3 base_fresnel = half3(base_fresnel_bias,1.0f - base_fresnel_bias,base_fresnel_power);
		half3 overlap_fresnel = half3(overlap_fresnel_bias,1.0f - overlap_fresnel_bias,overlap_fresnel_power);
		
		#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
			half2 base_phong_rim = half2(base_phong_rim_width,base_phong_rim_scale);
			half2 overlap_phong_rim = half2(overlap_phong_rim_width,overlap_phong_rim_scale);
		#endif
		
		#ifdef WORLD
			half3 light_direction = IN.texcoord_1.xyz;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 base_color = getDiffuseRim(base_shading,base_phong_rim,base_diffuse.xyz,base_normal,light_direction);
				half3 overlap_color = getDiffuseRim(overlap_shading,overlap_phong_rim,overlap_diffuse.xyz,overlap_normal,light_direction);
			#else
				half3 base_color = getDiffuse(base_shading,base_diffuse.xyz,base_normal,light_direction);
				half3 overlap_color = getDiffuse(overlap_shading,overlap_diffuse.xyz,overlap_normal,light_direction);
			#endif
			base_color += getSpecularFresnelNormalize(base_shading,base_fresnel,base_specular,base_normal,light_direction,camera_direction);
			overlap_color += getSpecularFresnelNormalize(overlap_shading,overlap_fresnel,overlap_specular,overlap_normal,light_direction,camera_direction);
		#else
			half4 light_direction = IN.texcoord_1;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 base_color = getDiffuseRimSpecularFresnelNormalizeAttenuate(base_shading,base_phong_rim,base_fresnel,base_diffuse.xyz,base_specular,base_normal,light_direction,camera_direction);
				half3 overlap_color = getDiffuseRimSpecularFresnelNormalizeAttenuate(overlap_shading,overlap_phong_rim,overlap_fresnel,overlap_diffuse.xyz,overlap_specular,overlap_normal,light_direction,camera_direction);
			#else
				half3 base_color = getDiffuseSpecularFresnelNormalizeAttenuate(base_shading,base_fresnel,base_diffuse.xyz,base_specular,base_normal,light_direction,camera_direction);
				half3 overlap_color = getDiffuseSpecularFresnelNormalizeAttenuate(overlap_shading,overlap_fresnel,overlap_diffuse.xyz,overlap_specular,overlap_normal,light_direction,camera_direction);
			#endif
		#endif
		
		half3 color = lerp(base_color,overlap_color,overlap);
		
	#endif
	
/******************************************************************************\
*
* Direct3D9
*
\******************************************************************************/

#elif DIRECT3D9
	
	#ifndef FRAGMENT_OVERLAP_LIGHT
		#define FRAGMENT_OVERLAP_LIGHT
		
		half4 mask_transform;
		half4 overlap_transform;
		half mask_value;
		half mask_threshold;
		half angle_value;
		half angle_threshold;
		half4 base_diffuse_color;
		half4 overlap_diffuse_color;
		half base_diffuse_scale;
		half overlap_diffuse_scale;
		half4 base_specular_color;
		half4 overlap_specular_color;
		half base_specular_scale;
		half overlap_specular_scale;
		half base_specular_power;
		half overlap_specular_power;
		half base_phong_rim_width;
		half overlap_phong_rim_width;
		half base_phong_rim_scale;
		half overlap_phong_rim_scale;
		half base_fresnel_bias;
		half overlap_fresnel_bias;
		half base_fresnel_power;
		half overlap_fresnel_power;
		
	#else
		
		#include <core/shaders/default/mesh/fragment_overlap_sample.h>
		
		half3 camera_direction = IN.texcoord_2.xyz;
		
		half3 base_shading = s_material_shading * half3(base_diffuse_scale,base_specular_scale,base_specular_power);
		half3 overlap_shading = s_material_shading * half3(overlap_diffuse_scale,overlap_specular_scale,overlap_specular_power);
		
		half3 base_fresnel = half3(base_fresnel_bias,1.0f - base_fresnel_bias,base_fresnel_power);
		half3 overlap_fresnel = half3(overlap_fresnel_bias,1.0f - overlap_fresnel_bias,overlap_fresnel_power);
		
		#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
			half2 base_phong_rim = half2(base_phong_rim_width,base_phong_rim_scale);
			half2 overlap_phong_rim = half2(overlap_phong_rim_width,overlap_phong_rim_scale);
		#endif
		
		#ifdef WORLD
			half3 light_direction = IN.texcoord_1.xyz;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 base_color = getDiffuseRim(base_shading,base_phong_rim,base_diffuse.xyz,base_normal,light_direction);
				half3 overlap_color = getDiffuseRim(overlap_shading,overlap_phong_rim,overlap_diffuse.xyz,overlap_normal,light_direction);
			#else
				half3 base_color = getDiffuse(base_shading,base_diffuse.xyz,base_normal,light_direction);
				half3 overlap_color = getDiffuse(overlap_shading,overlap_diffuse.xyz,overlap_normal,light_direction);
			#endif
			base_color += getSpecularFresnelNormalize(base_shading,base_fresnel,base_specular,base_normal,light_direction,camera_direction);
			overlap_color += getSpecularFresnelNormalize(overlap_shading,overlap_fresnel,overlap_specular,overlap_normal,light_direction,camera_direction);
		#else
			half4 light_direction = IN.texcoord_1;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 base_color = getDiffuseRimSpecularFresnelNormalizeAttenuate(base_shading,base_phong_rim,base_fresnel,base_diffuse.xyz,base_specular,base_normal,light_direction,camera_direction);
				half3 overlap_color = getDiffuseRimSpecularFresnelNormalizeAttenuate(overlap_shading,overlap_phong_rim,overlap_fresnel,overlap_diffuse.xyz,overlap_specular,overlap_normal,light_direction,camera_direction);
			#else
				half3 base_color = getDiffuseSpecularFresnelNormalizeAttenuate(base_shading,base_fresnel,base_diffuse.xyz,base_specular,base_normal,light_direction,camera_direction);
				half3 overlap_color = getDiffuseSpecularFresnelNormalizeAttenuate(overlap_shading,overlap_fresnel,overlap_diffuse.xyz,overlap_specular,overlap_normal,light_direction,camera_direction);
			#endif
		#endif
		
		half3 color = lerp(base_color,overlap_color,overlap);
		
	#endif
	
/******************************************************************************\
*
* PlayStation3
*
\******************************************************************************/

#elif PLAYSTATION3
	
	#ifndef FRAGMENT_OVERLAP_LIGHT
		#define FRAGMENT_OVERLAP_LIGHT
		
		uniform sampler2D s_texture_0 : TEXUNIT0;
		uniform sampler2D s_texture_1 : TEXUNIT1;
		uniform sampler2D s_texture_2 : TEXUNIT2;
		uniform sampler2D s_texture_3 : TEXUNIT3;
		uniform sampler2D s_texture_4 : TEXUNIT4;
		uniform sampler2D s_texture_5 : TEXUNIT5;
		uniform sampler2D s_texture_6 : TEXUNIT6;
		
		uniform half4 mask_transform;
		uniform half4 overlap_transform;
		uniform half mask_value;
		uniform half mask_threshold;
		uniform half angle_value;
		uniform half angle_threshold;
		uniform half4 base_diffuse_color;
		uniform half4 overlap_diffuse_color;
		uniform half base_diffuse_scale;
		uniform half overlap_diffuse_scale;
		uniform half4 base_specular_color;
		uniform half4 overlap_specular_color;
		uniform half base_specular_scale;
		uniform half overlap_specular_scale;
		uniform half base_specular_power;
		uniform half overlap_specular_power;
		uniform half base_phong_rim_width;
		uniform half overlap_phong_rim_width;
		uniform half base_phong_rim_scale;
		uniform half overlap_phong_rim_scale;
		uniform half base_fresnel_bias;
		uniform half overlap_fresnel_bias;
		uniform half base_fresnel_power;
		uniform half overlap_fresnel_power;
		
	#else
		
		#include <core/shaders/default/mesh/fragment_overlap_sample.h>
		
		half3 camera_direction = IN.texcoord_2.xyz;
		
		half3 base_shading = s_material_shading * half3(base_diffuse_scale,base_specular_scale,base_specular_power);
		half3 overlap_shading = s_material_shading * half3(overlap_diffuse_scale,overlap_specular_scale,overlap_specular_power);
		
		half3 base_fresnel = half3(base_fresnel_bias,1.0f - base_fresnel_bias,base_fresnel_power);
		half3 overlap_fresnel = half3(overlap_fresnel_bias,1.0f - overlap_fresnel_bias,overlap_fresnel_power);
		
		#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
			half2 base_phong_rim = half2(base_phong_rim_width,base_phong_rim_scale);
			half2 overlap_phong_rim = half2(overlap_phong_rim_width,overlap_phong_rim_scale);
		#endif
		
		#ifdef WORLD
			half3 light_direction = IN.texcoord_1.xyz;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 base_color = getDiffuseRim(base_shading,base_phong_rim,base_diffuse.xyz,base_normal,light_direction);
				half3 overlap_color = getDiffuseRim(overlap_shading,overlap_phong_rim,overlap_diffuse.xyz,overlap_normal,light_direction);
			#else
				half3 base_color = getDiffuse(base_shading,base_diffuse.xyz,base_normal,light_direction);
				half3 overlap_color = getDiffuse(overlap_shading,overlap_diffuse.xyz,overlap_normal,light_direction);
			#endif
			base_color += getSpecularFresnelNormalize(base_shading,base_fresnel,base_specular,base_normal,light_direction,camera_direction);
			overlap_color += getSpecularFresnelNormalize(overlap_shading,overlap_fresnel,overlap_specular,overlap_normal,light_direction,camera_direction);
		#else
			half4 light_direction = IN.texcoord_1;
			#ifdef USE_PHONG_RIM && QUALITY_MEDIUM
				half3 base_color = getDiffuseRimSpecularFresnelNormalizeAttenuate(base_shading,base_phong_rim,base_fresnel,base_diffuse.xyz,base_specular,base_normal,light_direction,camera_direction);
				half3 overlap_color = getDiffuseRimSpecularFresnelNormalizeAttenuate(overlap_shading,overlap_phong_rim,overlap_fresnel,overlap_diffuse.xyz,overlap_specular,overlap_normal,light_direction,camera_direction);
			#else
				half3 base_color = getDiffuseSpecularFresnelNormalizeAttenuate(base_shading,base_fresnel,base_diffuse.xyz,base_specular,base_normal,light_direction,camera_direction);
				half3 overlap_color = getDiffuseSpecularFresnelNormalizeAttenuate(overlap_shading,overlap_fresnel,overlap_diffuse.xyz,overlap_specular,overlap_normal,light_direction,camera_direction);
			#endif
		#endif
		
		half3 color = lerp(base_color,overlap_color,overlap);
		
	#endif
	
#endif
